<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="链接" prop="path">
				<el-input v-model="dataForm.path" placeholder="页面路径"></el-input>
			</el-form-item>



			<el-form-item label="白名单/鉴权名单" prop="isMenu">
				<template>
					<el-radio v-model="dataForm.isMenu" label="0">鉴权名单</el-radio>
					<el-radio v-model="dataForm.isMenu" label="2">白名单</el-radio>
				</template>
			</el-form-item>

			<el-form-item v-if="dataForm.isMenu == 0" label="用户权限" prop="path">
				<el-checkbox-group v-model="dataForm.roles">
					<el-checkbox label="ROLE_admin">管理员</el-checkbox>
					<el-checkbox label="ROLE_user">用户</el-checkbox>
				</el-checkbox-group>
			</el-form-item>

		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					path: '',
					isMenu: 1,
					roles: []
				},
				dataRule: {

					path: [{
						required: true,
						message: '链接不能为空',
						trigger: 'blur'
					}],
					isMenu: [{
						required: true,
						message: '判断是不是后台菜单表格不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.id) {
						this.$http({
							url: this.$http.adornUrl(`/menu/tauth/info/${this.dataForm.id}`),
							method: 'get',
							params: this.$http.adornParams()
						}).then(({
							data
						}) => {
							if (data && data.code === 0) {
								this.dataForm.path = data.tAuth.path
								this.dataForm.isMenu = data.tAuth.isMenu
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.request({
							url: '/api/auth/save',
							method: 'post',
							data: {
								'path': this.dataForm.path,
								'isMenu': this.dataForm.isMenu,
								'roles': this.dataForm.roles,
							}
						}).then((data) => {
							console.log(data)
							if (data && data.code == 200) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.visible = false
										this.$emit('refreshDataList')
									}
								})
							} else {
								this.$message.error(data.msg)
							}
						})
					}
				})
			}
		}
	}
</script>
